package com.sky.service.user.Impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.mapper.admin.DishMapper;
import com.sky.mapper.admin.SetmealMapper;
import com.sky.mapper.user.ShoppingCartMapper;
import com.sky.pojo.Setmeal;
import com.sky.pojo.ShoppingCart;
import com.sky.service.user.ShoppingCartService;
import com.sky.vo.DishVO;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车
 */
@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;


    /**
     * 添加购物车
     * @param dto
     */
    @Override
    public void add(ShoppingCartDTO dto) {
        log.info("添加购物车dishId{},setmealId{},dishFlavor{}", dto.getDishId(), dto.getSetmealId(), dto.getDishFlavor());
        //添加购物车，（购物车表新增数据）
        //注意：已经存在的菜品，如果新增，那么就在份数+1

        //创建购物车对象（前端需要选择）
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setDishId(dto.getDishId());
        shoppingCart.setSetmealId(dto.getSetmealId());
        shoppingCart.setDishFlavor(dto.getDishFlavor());
        shoppingCart.setCreateTime(LocalDateTime.now());
        shoppingCart.setUserId(BaseContext.getCurrentId());//设置用户id
        Long userId = shoppingCart.getUserId();             //获取用户id

        log.info("已设置用户id:{}", BaseContext.getCurrentId());

        //查询套餐中是否存在套餐或者菜品                         (根据用户id查询)
        ShoppingCart existCart = shoppingCartMapper.getByDishSetmeal(dto.getDishId(), dto.getSetmealId(), userId);
        if (existCart != null) {
            log.info("该套餐或菜品已存在，修改份数即可");
            //存在+1
            shoppingCartMapper.updeteNumById(existCart.getNumber() + 1, existCart.getId());
            shoppingCartMapper.updetedishFlavor(existCart.getDishFlavor(), existCart.getId());
            return;
        }
        log.info("设置默认份数");
        shoppingCart.setNumber(1);//不存在，就默认为1份

        //菜品的情况
        if (dto.getDishId() != null) {
            log.info("菜品不为null,设置菜品数据");
            DishVO dishVO = dishMapper.getShoppingDishById(dto.getDishId());

            log.info("根据Dish_Id查询菜品信息{}",dishVO);
            shoppingCart.setName(dishVO.getName());//套餐名字
            shoppingCart.setImage(dishVO.getImage());//图片
            shoppingCart.setAmount(dishVO.getPrice());//价格 来气
            shoppingCart.setId(dto.getDishId());  //传入的参数中的id

        }
        // 套餐的情况
        else {
            Setmeal setmeal = setmealMapper.getByIdSetmeal(dto.getSetmealId());
                shoppingCart.setName(setmeal.getName());//套餐名字
                shoppingCart.setImage(setmeal.getImage());//图片
                shoppingCart.setAmount(setmeal.getPrice());//价格
                shoppingCart.setId(dto.getSetmealId());
            }
        //插入购物车数据
        log.info("插入购物车数据{}", shoppingCart);
        shoppingCartMapper.addshoppingCaret(shoppingCart);
    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> getShopping() {
        log.info("查看购物车的id：{}", BaseContext.getCurrentId());//     BaseContext.getCurrentId()
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.getShopping(BaseContext.getCurrentId());//根据操作人id
        log.info("业务逻辑层返回操作人id：{}", BaseContext.getCurrentId());
        return shoppingCartList;
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        log.info("清空购物车人的id{}", BaseContext.getCurrentId());
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());//根据操作人的id
    }
}
